namespace DiLu.Expansion.File.Configs;

/// <summary>
///     搜索文件配置
/// </summary>
public abstract record SearchFileConfig
{
    /// <summary>
    ///     搜索文件配置
    /// </summary>
    /// <param name="directoryFullPath">目录完整路径</param>
    /// <param name="searchOption">搜索选项</param>
    /// <param name="searchPattern">搜索文件模式</param>
    protected SearchFileConfig(string directoryFullPath, SearchOption searchOption, string searchPattern)
    {
        ArgumentException.ThrowIfNullOrWhiteSpace(directoryFullPath, nameof(directoryFullPath));
        directoryFullPath = directoryFullPath.Replace('\\', '/').Trim();
        var rootPath = Path.GetPathRoot(directoryFullPath);
        ArgumentException.ThrowIfNullOrWhiteSpace(rootPath, nameof(rootPath));
        if (new DriveInfo(rootPath).DriveType == DriveType.Unknown) throw new ArgumentException("目录与系统目录不匹配");

        DirectoryFullPath = directoryFullPath.Trim();
        SearchOption = searchOption;
        SearchPattern = searchPattern;
    }

    /// <summary>
    ///     搜索文件配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="directoryRelativePath">目录相对路径</param>
    /// <param name="searchOption">搜索选项</param>
    /// <param name="searchPattern">搜索文件模式</param>
    protected SearchFileConfig(string rootDirectory, string fileGroupName, string directoryRelativePath,
        SearchOption searchOption, string searchPattern)
    {
        ArgumentException.ThrowIfNullOrWhiteSpace(rootDirectory, nameof(rootDirectory));
        rootDirectory = rootDirectory.Replace('\\', '/').Trim();
        var rootPath = Path.GetPathRoot(rootDirectory);
        ArgumentException.ThrowIfNullOrWhiteSpace(rootPath, nameof(rootPath));
        if (new DriveInfo(rootPath).DriveType == DriveType.Unknown) throw new ArgumentException("目录与系统目录不匹配");

        RootDirectory = rootDirectory;
        FileGroupName = $"{fileGroupName?.Trim()}";
        DirectoryRelativePath = $"{directoryRelativePath?.Trim().Replace('\\', '/').Trim()}";
        DirectoryFullPath = Path.Combine(RootDirectory, FileGroupName, DirectoryRelativePath);
        SearchOption = searchOption;
        SearchPattern = searchPattern;
    }

    /// <summary>
    ///     根目录
    /// </summary>
    public string RootDirectory { get; }

    /// <summary>
    ///     文件分组名称
    /// </summary>
    public string FileGroupName { get; }

    /// <summary>
    ///     目录相对路径
    /// </summary>
    public string DirectoryRelativePath { get; }

    /// <summary>
    ///     目录完整路径
    /// </summary>
    public string DirectoryFullPath { get; }

    /// <summary>
    ///     搜索选项
    /// </summary>
    public SearchOption SearchOption { get; }

    /// <summary>
    ///     搜索文件模式
    /// </summary>
    public string SearchPattern { get; }
}